﻿@{
    ViewBag.Title = "企业号部门";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    $(document).ready(function () {
        initialPage();
        GetGrid();
    });
    //重设(表格)宽高
    function initialPage() {
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 108.5);
            }, 200);
            e.stopPropagation();
        });
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        $gridTable.jqGrid({
            url: "../../WeChatManage/Organize/GetTreeListJson",
            datatype: "json",
            height: $(window).height() - 108.5,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'F_OrganizeId', hidden: true },
                { label: 'F_ParentId', name: 'F_ParentId', hidden: true },
                { label: 'F_FullName', name: 'F_FullName', hidden: true },
                {
                    label: "部门名称", width: 300, align: "left", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return rowObject.F_FullName;
                    }
                },
                { label: "部门编号", name: "F_EnCode", width: 150, align: "left", sortable: false },
                {
                    label: "同步状态", name: "F_IsSync", width: 80, align: "center", autowidth: false,
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == 1) {
                            return "<span class=\"label label-success\">已同步</span>";
                        } else {
                            return "<span class=\"label label-danger\">未同步</span>";
                        }
                    }
                },
                {
                    label: "同步时间", name: "F_SyncTime", width: 130, align: "center", autowidth: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                    }
                },
                { label: "备注", name: "F_Description", width: 200, align: "left", sortable: false }
            ],
            treeGrid: true,
            treeGridModel: "nested",
            ExpandColumn: "F_EnCode",
            rowNum: "all",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件设置
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            $gridTable.jqGrid('setGridParam', {
                postData: {
                    condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                    keyword: $("#txt_Keyword").val()
                }
            }).trigger('reloadGrid');
        });
        //查询回车事件
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }
    //一键同步
    function btn_synchronization() {
        var data = [];
        var getRowData = $("#gridTable").jqGrid("getRowData");
        for (var i = 0; i < getRowData.length; i++) {
            data.push({
                F_OrganizeId: getRowData[i].F_OrganizeId,
                F_FullName: getRowData[i].F_FullName,
                F_ParentId: getRowData[i].F_ParentId,
            })
        }
        $.ConfirmAjax({
            msg: "注：您确定要一键同步部门到微信企业号里面吗？",
            url: "../../WeChatManage/Organize/Synchronization",
            param: { organizeListJson: JSON.stringify(data) },
            success: function (data) {
                $("#gridTable").trigger("reloadGrid");
            }
        })
    }
</script>
<div class="titlePanel">
    <div class="title-search">
        <table>
            <tr>
                <td>
                    <div id="queryCondition" class="btn-group">
                        <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                        <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a data-value="FullName">部门名称</a></li>
                            <li><a data-value="EnCode">部门编号</a></li>
                        </ul>
                    </div>
                </td>
                <td style="padding-left: 2px;">
                    <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                </td>
                <td style="padding-left: 5px;">
                    <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                </td>
            </tr>
        </table>
    </div>
    <div class="toolbar">
        <div class="btn-group">
            <a id="lr-replace" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
        </div>
        <div class="btn-group">
            <a id="lr-synchronization" class="btn btn-default" onclick="btn_synchronization()"><i class="fa fa-refresh"></i>&nbsp;一键同步</a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
</div>
<div class="gridPanel">
    <table id="gridTable"></table>
</div>
